package com.spancer.usercenter.controller.user;

import com.spancer.usercenter.auth.CheckAuthorization;
import com.spancer.usercenter.auth.CheckLogin;
import com.spancer.usercenter.domain.entity.user.User;
import com.spancer.usercenter.service.user.UserService;
import com.spancer.usercenter.util.JwtOperator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

/**
 * author by: mars
 * Date: 2019/10/20 16:07
 * Description:
 */
@RestController
@RequestMapping("/users")
public class UserController {

    @Autowired
    private UserService userService;

    @GetMapping("/{id}")
    @CheckLogin // 校验token
    @CheckAuthorization("admin") // 校验权限
    public User findById(@PathVariable String id) {
        return this.userService.findById(Integer.parseInt(id));
    }

    @PostMapping("/save")
    public String save(@RequestBody User user) {
        System.out.println(user.toString());
        return "save:" + user.toString();
    }

    @PostMapping("/update")
    public String update(@RequestBody User user) {
        return "update:" + user.toString();
    }

    /**
     *
     * @Author mars
     * @Description 假登陆,生成token
     * @Date 2020/11/11 14:26
     * @param null
     * @return
     */
    @Autowired
    private JwtOperator jwtOperator;

    @GetMapping("gen-token")
    public String getToken(){
        Map<String,Object> userInfo = new HashMap<>();
        userInfo.put("id","mars");
        userInfo.put("wxNickName","spancer");
        userInfo.put("role","admin");
        return jwtOperator.generateToken(userInfo);
    }
}
